package com.dragon.mmochat;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dragon.mmochat.model.enumType.MmochatServerType;
import com.dragon.mmochat.model.serverListDao.DaoService;
import com.dragon.mmochat.service.ChargeOrderService;
import com.dragon.mmochat.service.MmochatBattleService;
import com.dragon.mmochat.service.MmochatBetService;
import com.dragon.mmochat.service.MmochatBigMoneyDealService;
import com.dragon.mmochat.service.MmochatChatService;
import com.dragon.mmochat.service.MmochatClientVerionServer;
import com.dragon.mmochat.service.MmochatDaoService;
import com.dragon.mmochat.service.MmochatDealService;
import com.dragon.mmochat.service.MmochatEquipService;
import com.dragon.mmochat.service.MmochatExitService;
import com.dragon.mmochat.service.MmochatFeedService;
import com.dragon.mmochat.service.MmochatFilterService;
import com.dragon.mmochat.service.MmochatFriendService;
import com.dragon.mmochat.service.MmochatGangService;
import com.dragon.mmochat.service.MmochatGmService;
import com.dragon.mmochat.service.MmochatHandle_SendChat;
import com.dragon.mmochat.service.MmochatHandle_SendMotion;
import com.dragon.mmochat.service.MmochatLifeSkillService;
import com.dragon.mmochat.service.MmochatMainService;
import com.dragon.mmochat.service.MmochatMapGuiderService;
import com.dragon.mmochat.service.MmochatMapService;
import com.dragon.mmochat.service.MmochatMotionService;
import com.dragon.mmochat.service.MmochatMultiServerPkService;
import com.dragon.mmochat.service.MmochatNewsService;
import com.dragon.mmochat.service.MmochatNpcService;
import com.dragon.mmochat.service.MmochatPackageService;
import com.dragon.mmochat.service.MmochatPetService;
import com.dragon.mmochat.service.MmochatPkMachineService;
import com.dragon.mmochat.service.MmochatPkTeamService;
import com.dragon.mmochat.service.MmochatPlayerBetService;
import com.dragon.mmochat.service.MmochatPlayerService;
import com.dragon.mmochat.service.MmochatPropService;
import com.dragon.mmochat.service.MmochatScheduleJobService;
import com.dragon.mmochat.service.MmochatServerListService;
import com.dragon.mmochat.service.MmochatShopService;
import com.dragon.mmochat.service.MmochatSkillService;
import com.dragon.mmochat.service.MmochatStatisticsService;
import com.dragon.mmochat.service.MmochatStoreService;
import com.dragon.mmochat.service.MmochatTaskService;
import com.dragon.mmochat.service.MmochatTeamService;
import com.dragon.mmochat.service.MmochatTempActivityService;
import com.dragon.mmochat.service.MmochatTowerService;
import com.dragon.mmochat.service.ServerListStatistics;
import com.dragon.mmochat.util.MmochatUtil;
import com.skymobi.SkymobiMain;
import com.skymobi.service.currency.DirectChargeService;

public class MmochatMain {
	private static MmochatDaoService daoService = null;
	public static DaoService serverListDaoService = null;
	public static int moduleInstID; // 实例号
	public static int clientVersion = -1; // 终端版本号
	public static int serverVersion = (int) (System.currentTimeMillis()); // 服务端版本号
	public static String serverName = null; // 服务器名
	public static Map<Integer, Integer> serverListInfo = null; // 所有区服：ID-->实例号
	public static MmochatServerType serverType = MmochatServerType.游戏服务器;
	public static int pkServerInstID = 0; // 跨服战实例号
	public static boolean openMergeServer = false; // true:则作为合区程序；正常使用时要设为false
	// 值为1：则只进行元宝订单完结；合服后消失的区中运行此项；
	// 值为2：则只进行role表和gang表的处理；合服后目标区运行此项；
	public static int mergeServerStep = 1;
	public static Object[] skymobiServices;

	public static void main(String[] args) throws Exception {
		System.out.println("enter");
		if (args.length < 4) {
			throw new RuntimeException(
					"启动参数不足四个参数,请设置如"
							+ "<ModuleID=0x1000 ModuleInstID=0x1001 ServerType=gameServer TargetPkServerInstID=0x1002>"
							+ "格式的启动参数!ServerType可选值：gameServer,pkServer。"
							+ "其中pkServer表示跨服战服务器!TargetPkServerInstID表示此服务器连接的跨服战服务器的实例号");
		}

		// 获取实例号
		String cs = args[1];
		int index = cs.indexOf("0x");
		if (index == -1) {
			throw new RuntimeException(
					"启动参数格式不符,模块号和实例号请使用0x作为前缀的16进制数表示,"
							+ "如<ModuleID=0x1000 ModuleInstID=0x1001 ServerType=gameServer TargetPkServerInstID=0x1002>!");
		}
		cs = cs.substring(index + 2);
		try {
			moduleInstID = (Integer.parseInt(cs, 16));
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(
					"启动参数格式不符,请设置如<ModuleID=0x1000 ModuleInstID=0x1001 ServerType=gameServer TargetPkServerInstID=0x1002>格式的启动参数!");
		}

		// 获取服务器类型
		String ServerTypeStr = args[2];
		index = ServerTypeStr.indexOf("=");
		if (index == -1) {
			throw new RuntimeException(
					"启动参数格式不符,请设置如<ModuleID=0x1000 ModuleInstID=0x1001 ServerType=gameServer TargetPkServerInstID=0x1002>格式的启动参数!");
		}
		ServerTypeStr = ServerTypeStr.substring(index + 1);
		if (ServerTypeStr != null) {
			if (ServerTypeStr.equals("pkServer")) {
				serverType = MmochatServerType.跨服战服务器;
			} else if (ServerTypeStr.equals("gameServer")) {
				serverType = MmochatServerType.游戏服务器;
			}
		}

		// 获取跨服战服务器类型
		if (serverType == MmochatServerType.游戏服务器) {
			String pkServerInstIDStr = args[3];
			index = pkServerInstIDStr.indexOf("0x");
			if (index == -1) {
				throw new RuntimeException(
						"启动参数格式不符,模块号和实例号请使用0x作为前缀的16进制数表示,"
								+ "如<ModuleID=0x1000 ModuleInstID=0x1001 ServerType=gameServer TargetPkServerInstID=0x1002>!");
			}
			pkServerInstIDStr = pkServerInstIDStr.substring(index + 2);
			try {
				pkServerInstID = (Integer.parseInt(pkServerInstIDStr, 16));
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException(
						"启动参数格式不符,请设置如<ModuleID=0x1000 ModuleInstID=0x1001 ServerType=gameServer TargetPkServerInstID=0x1002>格式的启动参数!");
			}
		}

		{
			// 装配数据库类
			if (serverType.isHasDao()) {
				ApplicationContext dao = new ClassPathXmlApplicationContext(
						"daoContext.xml");
				daoService = (MmochatDaoService) dao
						.getBean("mmochatDaoService");
			}
			ServerListStatistics serverListStatistics = null;
			if (moduleInstID == MmochatConstant.serverListModuleId) {
				// 列表服务器
				ApplicationContext dao = new ClassPathXmlApplicationContext(
						"serverListDaoContext.xml");
				serverListDaoService = (DaoService) dao.getBean("daoService");
				serverListStatistics = new ServerListStatistics();
			}

			// 游戏服务器，装配所有服务
			ApplicationContext context = new ClassPathXmlApplicationContext(
					"applicationContext.xml");

			MmochatFilterService mmochatFilterService = (MmochatFilterService) context
					.getBean("mmochatFilterService");

			MmochatMainService mmochatMainService = (MmochatMainService) context
					.getBean("mmochatMainService");

			MmochatPetService mmochatPetService = (MmochatPetService) context
					.getBean("mmochatPetService");

			MmochatMapService mmochatMapService = (MmochatMapService) context
					.getBean("mmochatMapService");

			MmochatNpcService mmochatNpcService = (MmochatNpcService) context
					.getBean("mmochatNpcService");

			MmochatMotionService mmochatMotionService = (MmochatMotionService) context
					.getBean("mmochatMotionService");

			MmochatDaoService mmochatDaoService = daoService;

			MmochatBattleService mmochatBattleService = (MmochatBattleService) context
					.getBean("mmochatBattleService");

			MmochatTaskService mmochatTaskService = (MmochatTaskService) context
					.getBean("mmochatTaskService");

			MmochatPackageService mmochatPackageService = (MmochatPackageService) context
					.getBean("mmochatPackageService");

			MmochatDealService mmochatDealService = (MmochatDealService) context
					.getBean("mmochatDealService");

			MmochatTeamService mmochatTeamService = (MmochatTeamService) context
					.getBean("mmochatTeamService");

			MmochatEquipService mmochatEquipService = (MmochatEquipService) context
					.getBean("mmochatEquipService");

			MmochatPlayerService mmochatPlayerService = (MmochatPlayerService) context
					.getBean("mmochatPlayerService");

			MmochatFriendService mmochatFriendService = (MmochatFriendService) context
					.getBean("mmochatFriendService");

			MmochatScheduleJobService mmochatScheduleJobService = (MmochatScheduleJobService) context
					.getBean("mmochatScheduleJobService");

			MmochatChatService mmochatChatService = (MmochatChatService) context
					.getBean("mmochatChatService");

			MmochatStoreService mmochatStoreService = (MmochatStoreService) context
					.getBean("mmochatStoreService");

			MmochatPropService mmochatPropService = (MmochatPropService) context
					.getBean("mmochatPropService");

			MmochatServerListService mmochatServerListService = (MmochatServerListService) context
					.getBean("mmochatServerListService");

			MmochatGangService mmochatGangService = (MmochatGangService) context
					.getBean("mmochatGangService");

			MmochatPkTeamService mmochatPkTeamService = (MmochatPkTeamService) context
					.getBean("mmochatPkTeamService");

			MmochatClientVerionServer mmochatClientVerionServer = (MmochatClientVerionServer) context
					.getBean("mmochatClientVerionServer");

			MmochatSkillService mmochatSkillService = (MmochatSkillService) context
					.getBean("mmochatSkillService");

			// MmochatManageService mmochatManageService =
			// (MmochatManageService) context
			// .getBean("mmochatManageService");

			MmochatBetService mmochatBetService = (MmochatBetService) context
					.getBean("mmochatBetService");

			MmochatExitService mmochatExitService = (MmochatExitService) context
					.getBean("mmochatExitService");

			MmochatGmService mmochatGmService = (MmochatGmService) context
					.getBean("mmochatGmService");

			MmochatFeedService mmochatFeedService = (MmochatFeedService) context
					.getBean("mmochatFeedService");

			MmochatStatisticsService mmochatStatisticsService = (MmochatStatisticsService) context
					.getBean("mmochatStatisticsService");

			MmochatBigMoneyDealService mmochatBigMoneyDealService = (MmochatBigMoneyDealService) context
					.getBean("mmochatBigMoneyDealService");

			MmochatTempActivityService mmochatTempActivityService = (MmochatTempActivityService) context
					.getBean("mmochatTempActivityService");

			MmochatShopService mmochatShopService = (MmochatShopService) context
					.getBean("mmochatShopService");

			MmochatMapGuiderService mmochatMapGuiderService = (MmochatMapGuiderService) context
					.getBean("mmochatMapGuiderService");

			MmochatTowerService mmochatTowerService = (MmochatTowerService) context
					.getBean("mmochatTowerService");

			MmochatMultiServerPkService mmochatMultiServerPkService = (MmochatMultiServerPkService) context
					.getBean("mmochatMultiServerPkService");

			MmochatLifeSkillService mmochatLifeSkillService = (MmochatLifeSkillService) context
					.getBean("mmochatLifeSkillService");

			// 卡密直付
			ChargeOrderService service = new ChargeOrderService();
			DirectChargeService chargeService = new DirectChargeService();
			chargeService.setNoticeProcessor(service);

			List<Object> skymobiServiceList = new ArrayList<Object>();

			skymobiServices = new Object[] { mmochatFilterService,
					mmochatMainService, mmochatPetService, mmochatDaoService,
					mmochatMapService, mmochatNpcService, mmochatMotionService,
					mmochatBattleService, mmochatTaskService,
					mmochatPackageService, mmochatDealService,
					mmochatTeamService, mmochatEquipService,
					mmochatPlayerService, mmochatFriendService,
					mmochatScheduleJobService, mmochatChatService,
					mmochatStoreService, mmochatPropService,
					mmochatServerListService, mmochatGangService,
					mmochatPkTeamService, mmochatClientVerionServer,
					mmochatSkillService, mmochatBetService, mmochatExitService,
					mmochatGmService, chargeService, mmochatFeedService,
					mmochatStatisticsService, mmochatBigMoneyDealService,
					mmochatTempActivityService, mmochatShopService,
					mmochatMapGuiderService, mmochatTowerService,
					mmochatMultiServerPkService, serverListDaoService,
					serverListStatistics, mmochatLifeSkillService,
					new MmochatNewsService(), new MmochatPkMachineService(),
					new MmochatPlayerBetService() };

			for (Object ser : skymobiServices) {
				if (ser != null) {
					skymobiServiceList.add(ser);
				}
			}
			skymobiServices = skymobiServiceList.toArray();

		}
		if (openMergeServer) {
			System.out.println("finish all!");
			System.exit(0);
			return;
		}
		System.out
				.println("-----------------------启动发送聊天功能模块----------------------");
		String[] chatArgs = new String[] {
				"ModuleID=0x"
						+ Integer.toHexString(MmochatConstant.serverModuleId),
				"ModuleInstID=0x" + Integer.toHexString(moduleInstID + 0x10) };
		Object[] chatHandleService = new Object[] { new MmochatHandle_SendChat() };
		new Thread(new MmochatMain().new runMain(chatArgs, chatHandleService))
				.start();

		System.out
				.println("-----------------------启动发送人物走动功能模块----------------------");
		String[] motionArgs = new String[] {
				"ModuleID=0x"
						+ Integer.toHexString(MmochatConstant.serverModuleId),
				"ModuleInstID=0x" + Integer.toHexString(moduleInstID + 0x20) };
		Object[] motionHandleService = new Object[] { new MmochatHandle_SendMotion() };
		new Thread(new MmochatMain().new runMain(motionArgs,
				motionHandleService)).start();

		System.out
				.println("-----------------------启动主程序----------------------");
		String[] newArgs = new String[] { args[0], args[1] };
		new Thread(new MmochatMain().new runMain(newArgs, skymobiServices))
				.start();

		// new SkymobiMain(args, skymobiServices,
		// MmochatConstant.serverModuleId);

		System.out.println("run");
		System.out.println("moduleId=0x"
				+ Integer.toHexString(MmochatConstant.serverModuleId)
				+ ",moduleInstID=0x" + Integer.toHexString(moduleInstID)
				+ ",serverType=" + serverType.toString() + ",pkServerInstID=0x"
				+ Integer.toHexString(pkServerInstID));
		System.out.println("serverVersion=" + serverVersion);
	}

	class runMain extends Thread {
		String[] args;
		Object[] skymobiServices;

		public runMain(String[] args, Object[] skymobiServices) {
			this.args = args;
			this.skymobiServices = skymobiServices;
		}

		public void run() {
			new SkymobiMain(args, skymobiServices,
					MmochatConstant.serverModuleId);
		}
	}

	public static MmochatDaoService getDaoService() {
		return daoService;
	}

	public static int getModuleInstID() {
		return moduleInstID;
	}

	public static int getClientVersion() {
		return clientVersion;
	}

	public static void setClientVersion(int clientVersion) {
		MmochatMain.clientVersion = clientVersion;
	}

	public static int getServerVersion() {
		return serverVersion;
	}

	public static String getServerName() {
		return serverName;
	}

	public static void setServerName(String serverName) {
		MmochatMain.serverName = serverName;
	}

	public static Map<Integer, Integer> getServerListInfo() {
		return serverListInfo;
	}

	public static void setServerListInfo(Map<Integer, Integer> serverListInfo) {
		MmochatMain.serverListInfo = new ConcurrentHashMap<Integer, Integer>(
				serverListInfo);
	}

	public static Integer getInstanceIdByServerId(int id) {
		if (serverListInfo == null) {
			return null;
		}
		return serverListInfo.get(id);
	}

	public static Integer getThisServerId() {
		if (serverListInfo == null) {
			return null;
		}
		for (Entry<Integer, Integer> entry : serverListInfo.entrySet()) {
			Integer moduleId = entry.getValue();
			if (moduleId != null && moduleId == moduleInstID) {
				return entry.getKey();
			}
		}
		return null;
	}

	public static Integer getServerIdByInstanceId(int instId) {
		if (serverListInfo == null) {
			return null;
		}
		for (Entry<Integer, Integer> entry : serverListInfo.entrySet()) {
			Integer moduleId = entry.getValue();
			if (moduleId != null && moduleId == instId) {
				return entry.getKey();
			}
		}
		return null;
	}
}
